<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="-mb-15px" label-width="68px">
      <el-form-item label="订单编号" prop="orderNum">
        <el-input v-model="queryParams.orderNum" class="!w-240px" clearable placeholder="请输入订单编号"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" class="!w-240px" placeholder="请选择订单状态" clearable>
          <el-option v-for="item in getIntDictOptions(DICT_TYPE.ORDER_STATUS)" :key="item.value" :label="item.label"
            :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-240px" end-placeholder="结束日期"
          start-placeholder="开始日期" type="daterange" value-format="YYYY-MM-DD HH:mm:ss" />
      </el-form-item>
      <el-form-item label="交付人" prop="testDeliverPeopleNickname">
        <el-input v-model="queryParams.testDeliverPeopleNickname" class="!w-240px" clearable placeholder="请输入交付人"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="订单编号" align="center" prop="orderNum" :show-overflow-tooltip="true">
        <template #default="scope">
          <span style="color: #409eff;cursor: pointer;" @click="openForm(scope.row.orderId)">
            {{ scope.row.orderNum }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="订单类型" align="center" prop="orderContent" :show-overflow-tooltip="true">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ORDER_CONTENT" :value="scope.row.orderContent" />
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ORDER_STATUS" :value="scope.row.orderStatus" />
        </template>
      </el-table-column>
      <el-table-column label="订单金额" align="center" prop="quote">
        <template #default="scope">
          <span>{{ scope.row.quote === null ? '-' : scope.row.quote + '元' }} </span>
        </template>
      </el-table-column>
      <el-table-column label="期望交付时间" align="center" prop="expectedDate" :formatter="dateFormatter2" />
      <el-table-column label="交付人" align="center" prop="testDeliverPeopleNickname" width="120" />
      <el-table-column label="交付时间" align="center" prop="testDeliverTime" :formatter="dateFormatter" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" />
      <el-table-column :width="300" align="center" label="操作">
        <template #default="scope">
          <el-button link type="primary" @click="openForm(scope.row.orderId)">
            订单详情
          </el-button>
          <el-button link type="primary" @click="openEdit(scope.row)"
            v-if="(scope.row.orderStatus >= 3 && scope.row.payMethod === 0 && scope.row.orderStatus != 6) || scope.row.payMethod === 1">
            测评交付
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.orderId)" v-if="scope.row.orderStatus != 6">
            关闭订单
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNo" :total="total"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：详情 -->
  <OrderDetail ref="formRef" @success="getList" />
  <!-- 表单弹窗：编辑报价 -->
  <OrderEdit ref="editRef" @success="getList" />
  <!-- 关闭订单 -->
  <CloseReason ref="closeReasonRef" @success="getList" />
</template>
<script setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'
import * as OrderApi from '@/api/order'
import OrderDetail from '../orderDetail.vue'
import OrderEdit from './orderEdit.vue'
import CloseReason from '../closeReason.vue'

defineOptions({ name: 'Business' })

const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderNum: '',
  orderStatus: undefined,
  createTime: [],
  testDeliverPeopleNickname: ''
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrderApi.getTestPage(queryParams)
    data.records.forEach((item) => {
      if (item.quoteFile) {
        const File = JSON.parse(item.quoteFile)
        item.url = File[0].url
      } else {
        item.url = ''
      }
    })
    list.value = data.records
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 查看操作 */
const formRef = ref()
const openForm = (row) => {
  formRef.value.open(row)
}

const closeReasonRef = ref()
/** 删除按钮操作 */
const handleDelete = async (orderId) => {
  closeReasonRef.value.open(orderId)
}

/** 编辑 */
const editRef = ref()
const openEdit = (row) => {
  editRef.value.open(row)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
